<script type="text/javascript">
	function MessageCtrl(CtrlUtil, $http, $element, $injector, $q, modalService, $scope) {
		var ctrl = this;
		ctrl.pageNumber = 0;
		// 查詢
		this.query = function() {
			ctrl.entities && (ctrl.entities.content = {})
			var p = CtrlUtil.getQueryParams(ctrl)
			$http.post(':root/message/queryMessage', p).success(function(r) {
				ctrl.entities = r;
			})
		};
		this.reset = function() {
			CtrlUtil.resetQueryParams(ctrl);
		};
		this.dialogPop = function(tpl, add, edit, msg, option) {
			var q = $q.defer();
			var content = $element.find(tpl).clone();
			var method = edit ? 'confirm' : 'show'
			return modalService[method]({
				title : add ? '添加' : edit ? '编辑' : '查看',
				readOnly : !add && !edit,
				content : content,
				msg : msg,
				search : {
					name : msg.fbdxName
				},
				ctrl : ctrl
			}, option).then(function(r) {
				q.resolve(msg);
			})
			return q.promise;
		};
		// 添加发布消息
		this.dialogAdd = function() {
			var msg = {
				fssj : new Date(),
				fsfs : "1",
				fbr : '系统管理员',
				fsr:'['+$scope.$user.uid+']'+$scope.$user.name
			};
			ctrl.dialogPop('.tpl>form', true, true, msg, {
				backdrop : false
			}).then(function(r) {
				$http.post(':root/message/addMessage', msg).success(function(r) {
					if (r) {
						$injector.get('modalService').succ('添加成功。', 1500);
						ctrl.current = r;
						ctrl.query();
					}
				});
			});
		};
		// 查看
		this.dialogView = function(msg) {
			ctrl.dialogPop('.tpl>form', false, false, msg);
		};
		// 編輯消息
		this.dialogEdit = function(msg) {
			ctrl.dialogPop('.tpl>form', false, true, msg, {
				backdrop : false
			}).then(function(r) {
				$http.post(':root/message/editMessage', msg).success(function(r) {
					if (r) {
						$injector.get('modalService').succ('编辑成功。', 1500);
						ctrl.current = r;
						ctrl.query();
					}
				});
			});
		};
		this.searchStudent = function(name, target) {
			$http.post(':root/trainee/queryRawStudent', {
				name : name
			}).success(function(r) {
				target = target || ctrl;
				target.students = r;
			});
		};
	}
</script>
<!-- Search area -->
<div ng-controller="MessageCtrl as ctrl" ng-init="ctrl.query()">
	<!-- Search area -->
	<div class="panel panel-cys">
		<div class="panel-heading">
			<h3 class="panel-title">
				<span class="glyphicon glyphicon-search"></span>
				信息查询
			</h3>
		</div>
		<div class="panel-body">
			<div class="container-fluid">
				<form class="form-horizontal row">
					<div class="form-group col-md-6 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<input type="text" class="form-control" placeholder="输入学号/姓名模糊查询发送对象" ng-model="ctrl.nameQueryParam">
						</div>
					</div>
					<div class="form-group col-md-6 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<select chosen ng-model="ctrl.sendTypeQueryParam" class="form-control">
								<option value="">请选择发送方式</option>
								<option value="0">系统自动</option>
								<option value="1">手动</option>
							</select>
						</div>
					</div>
				</form>
			</div>
			<div class="row pull-right">
				<div class="col-md-12">
					<button ng-click="ctrl.query()" type="submit" class="btn btn-cys btn-sm">
						<span class="glyphicon glyphicon-search"></span>
						查询
					</button>
					<button ng-click="ctrl.reset()" type="reset" class="btn btn-default btn-sm">
						<span class="glyphicon glyphicon-repeat"></span>
						重置
					</button>
				</div>
			</div>
		</div>
	</div>
	<!-- Search area END -->

	<!-- operation area -->
	<button type="button" class="btn btn-cys btn-sm" ng-click="ctrl.dialogAdd()">
		<span class="glyphicon glyphicon-plus"></span>
		新增
	</button>
	<div class="ny-top-r pull-right" ng-include="':root/components/page.html'"></div>
	<!-- operation area END -->


	<!-- data grid area -->
	<div style="padding-top: 10px;">
		<table class="table table-bordered table-hover table-striped">
			<tr>
				<th>发送人</th>
				<th>发送时间</th>
				<th>发送内容</th>
				<th>发送对象</th>
				<th>发送方式</th>
				<th>操作</th>
			</tr>
			<tr ng-repeat="item in ctrl.entities.content">
				<td>{{item.fsr}}</td>
				<td title="{{item.fssj|date:'yyyy-MM-dd HH:mm'}}">{{item.fssj|date:'yyyy-MM-dd HH:mm'}}</td>
				<td>{{item.fsnr}}</td>
				<td>{{item.fbdxName}}</td>
				<td>{{item.fsfs==0?'系统自动':'手动'}}</td>
				<td>
					<a ng-show="item.fsfs==0" href="javascript:;" class="remove-underline" ng-click="ctrl.dialogView(item)">查看</a>
					<a ng-show="item.fsfs==1" href="javascript:;" class="remove-underline" ng-click="ctrl.dialogEdit(item)">编辑</a>
				</td>
			</tr>
		</table>
		<p>
			共有记录：
			<span>{{ctrl.entities.totalElements}}</span>
			条
		</p>
	</div>
	<!-- data grid area END -->

	<div ng-non-bindable class="tpl hidden">
		<form class="form-horizontal">
			<div class="form-group">
				<label class="col-md-2 control-label">发送对象</label>
				<div class="col-md-10 open">
					<input type="text" ng-change="ctrl.searchStudent(search.name,search)" ng-model="search.name" class="form-control"
						ng-disabled="readOnly" placeholder="按学号/姓名模糊搜索">
					<ul class="dropdown-menu" ng-show="search.students.content.length>0">
						<li>
							<a href="javascript:;" ng-click="msg.fbdx='all';search.name='全部';msg.fbdxName=search.name;search.students=null;">全部</a>
						</li>
						<li ng-repeat="t in search.students.content">
							<a href="javascript:;" ng-click="msg.fbdx=t.id;search.name='['+t.xh+']'+t.name;msg.fbdxName=search.name;search.students=null;">[{{t.xh}}]{{t.name}}</a>
						</li>
					</ul>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-2 control-label">发送方式</label>
				<div class="col-md-4">
					<select disabled="disabled" ng-model="msg.fsfs" class="form-control">
						<option value="0">系统自动</option>
						<option value="1">手动</option>
					</select>
				</div>
				<label class="col-md-2 control-label">发送人</label>
				<div class="col-md-4">
					<input type="text" class="form-control" ng-model="msg.fsr" disabled="disabled">
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-2 control-label">发送内容</label>
				<div class="col-md-10">
					<textarea class="form-control" rows="5" ng-model="msg.fsnr" ng-disabled="readOnly" required="required"></textarea>
				</div>
			</div>
		</form>
	</div>

</div>